<?php

class Date
{

    // int
    private $IdDate;
    // varchar
    private $TitreDate;
    // varchar
    private $ValeurDate;
    // text
    private $TypeDate;
    // int
    private $IdPromo;


    public function __construct($id = 0)
    {
        if (empty($id))
        {
            $this->IdDate = 0;
            $this->TitreDate = "";
            $this->ValeurDate = "";
            $this->TypeDate = "";
            $this->IdPromo = 0;
            $this->requeteOk = true;
            $this->errormsg = "";
        }
        else
        {
            $sql = SQL::getInstance()->getConnection();
            $query = "SELECT * FROM Date WHERE IdDate = :primary";
            $c = $sql->prepare($query);
            $c->bindParam(":primary", $id, PDO::PARAM_INT);
            if ($c->execute())
            {
                $result = $c->fetch(PDO::FETCH_OBJ);
                if (!empty($result))
                {
                    $this->IdDate = $result->IdDate;
                    $this->TitreDate = $result->TitreDate;
                    $this->ValeurDate = $result->ValeurDate;
                    $this->TypeDate = $result->TypeDate;
                    $this->IdPromo = $result->IdPromo;
                    $this->requeteOk = true;
                    $this->errormsg = "Date trouv&eacute;e";
                }
                else
                {
                    $this->requeteOk = false;
                    $this->errormsg = "Ce Date n'existe pas !";
                }
            }
            else
            {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
            }
        }
    }

    public function insert()
    {
        $sql = SQL::getInstance()->getConnection();
        $query = "
			INSERT INTO Date
			(
				TitreDate,
				ValeurDate,
				TypeDate,
				IdPromo
			)
			VALUES
			(
				:TitreDate,
				:ValeurDate,
				:TypeDate,
				:IdPromo
		)";
        $sql->beginTransaction();
        try
        {
            $c = $sql->prepare($query);
            $c->bindParam(":TitreDate", $this->TitreDate, PDO::PARAM_STR, 255);
            $c->bindParam(":ValeurDate", $this->ValeurDate, PDO::PARAM_STR, 255);
            $c->bindParam(":TypeDate", $this->TypeDate, PDO::PARAM_STR);
            $c->bindParam(":IdPromo", $this->IdPromo, PDO::PARAM_INT);
            if ($c->execute())
            {
                $this->IdDate = $sql->lastInsertId();
                $sql->commit();
                $c->closeCursor();
                $this->requeteOk = true;
                $this->errormsg = "Insertion reussie !";
            }
            else
            {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
                $sql->rollback();
                $c->closeCursor();
                $this->IdDate = 0;
            }
        }
        catch (PDOException $Exception)
        {
            $this->requeteOk = false;
            $this->errormsg = $c->errorInfo();
            $sql->rollback();
            $c->closeCursor();
            $this->IdDate = 0;
        }
    }

    public function update()
    {
        $sql = SQL::getInstance()->getConnection();
        $query =
                "UPDATE Date SET
				TitreDate = :TitreDate,
				ValeurDate = :ValeurDate,
				TypeDate = :TypeDate,
				IdPromo = :IdPromo
			WHERE
				IdDate = :IdDate
		";
        $sql->beginTransaction();
        try
        {
            $c = $sql->prepare($query);

            $c->bindParam(":IdDate", $this->IdDate, PDO::PARAM_INT);
            $c->bindParam(":TitreDate", $this->TitreDate, PDO::PARAM_STR, 255);
            $c->bindParam(":ValeurDate", $this->ValeurDate, PDO::PARAM_STR, 255);
            $c->bindParam(":TypeDate", $this->TypeDate, PDO::PARAM_STR);
            $c->bindParam(":IdPromo", $this->IdPromo, PDO::PARAM_INT);

            if ($c->execute())
            {
                $sql->commit();
                $c->closeCursor();
                $this->requeteOk = true;
                $this->errormsg = "Modification reussie!";
            }
            else
            {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
                $sql->rollback();
                $c->closeCursor();
            }
        }
        catch (PDOException $Exception)
        {
            $this->requeteOk = false;
            $this->errormsg = $c->errorInfo();
            $sql->rollback();
            $c->closeCursor();
        }
        return $this->requeteOk;
    }

    public function delete()
    {
        $sql = SQL::getInstance()->getConnection();
        $query = "DELETE FROM Date WHERE IdDate = :primary";
        $c = $sql->prepare($query);
        $c->bindParam(":primary", $this->IdDate, PDO::PARAM_INT);
        $c->execute();
    }

    public function getIdDate()
    {
        return $this->IdDate;
    }

    public function getTitreDate()
    {
        return $this->TitreDate;
    }

    public function getValeurDate()
    {
        return $this->ValeurDate;
    }

    public function getTypeDate()
    {
        return $this->TypeDate;
    }

    public function getIdPromo()
    {
        return $this->IdPromo;
    }

    public function setIdDate($IdDate)
    {
        $errTab = array("boolOk" => true, "errmsg" => "");
        if (isset($IdDate) && !empty($IdDate))
        {
            $this->IdDate = $IdDate;
        }
        else
        {
            $this->IdDate = 0;
            $this->boolOK = false;
            $this->errormsg = "Le champ IdDate ne peut etre vide";
        }
        return $errTab;
    }

    public function setTitreDate($TitreDate)
    {
        $errTab = array("boolOk" => true, "errmsg" => "");
        if (isset($TitreDate) && !empty($TitreDate))
        {
            $this->TitreDate = $TitreDate;
        }
        else
        {
            if (!isset($TitreDate))
            {
                $errTab["errmsg"] = "Le TitreDate ne peut &ecirc;tre vide !";
            }
            elseif (empty($TitreDate))
            {
                $errTab["errmsg"] = "Le TitreDate ne peut &ecirc;tre vide !";
            }
            elseif (strlen($TitreDate) > 255)
            {
                $errTab["errmsg"] = "Le TitreDate doit &ecirc;tre compris entre 1 et 255 caract&egrave;res !";
            }
            $this->TitreDate = "";
            $this->boolOK = false;
        }
        return $errTab;
    }

    public function setValeurDate($ValeurDate)
    {
        $errTab = array("boolOk" => true, "errmsg" => "");
        if (isset($ValeurDate) && !empty($ValeurDate))
        {
            $this->ValeurDate = $ValeurDate;
        }
        else
        {
            if (!isset($ValeurDate))
            {
                $errTab["errmsg"] = "Le ValeurDate ne peut &ecirc;tre vide !";
            }
            elseif (empty($ValeurDate))
            {
                $errTab["errmsg"] = "Le ValeurDate ne peut &ecirc;tre vide !";
            }
            elseif (strlen($ValeurDate) > 255)
            {
                $errTab["errmsg"] = "Le ValeurDate doit &ecirc;tre compris entre 1 et 255 caract&egrave;res !";
            }
            $this->ValeurDate = "";
            $this->boolOK = false;
        }
        return $errTab;
    }

    public function setTypeDate($TypeDate)
    {
        $this->TypeDate = $TypeDate;
    }

    public function setIdPromo($IdPromo)
    {
        $errTab = array("boolOk" => true, "errmsg" => "");
        if (isset($IdPromo) && !empty($IdPromo) )
        {
            $this->IdPromo = $IdPromo;
        }
        else
        {
            $this->IdPromo = 0;
            $this->boolOK = false;
            $this->errormsg = "Le champ IdPromo ne peut etre vide";
        }
        return $errTab;
    }

    static public function getList()
    {
        $sql = SQL::getInstance()->getConnection();
        $query = "SELECT * FROM Date GROUP BY Date.IdPromo ASC, Date.ValeurDate ASC";
        $c = $sql->query($query);
        $result = $c->fetchAll(PDO::FETCH_OBJ);
        if (!empty($result))
        {
            return $result;
        }
        return array();
    }

    static public function getListByPromo($idpromo)
    {
        $sql = SQL::getInstance()->getConnection();
        $query = "SELECT * FROM Date WHERE Date.IdPromo = :idP GROUP BY Date.ValeurDate ASC";
        $sql->beginTransaction();
        try
        {
            $c = $sql->prepare($query);
            $c->bindParam(":idP", $idpromo, PDO::PARAM_INT);
            if ($c->execute())
            {
                $result = $c->fetchAll(PDO::FETCH_OBJ);
                return $result;
            }
            else
            {
                return array();
            }
        }
        catch (PDOException $Exception)
        {
            return array();
        }
    }

    static public function getListGroupByPromo()
    {
        $sql = SQL::getInstance()->getConnection();
        $query = "SELECT * FROM Date GROUP BY Date.IdPromo ASC, Date.ValeurDate ASC";
        $c = $sql->query($query);
        $result = $c->fetchAll(PDO::FETCH_OBJ);
        if (!empty($result))
        {
            $dates = array();
            foreach( $result as $date )
            {
                $dates[$date->IdPromo][$date->IdDate] = $date;
            }
            return $dates;
        }
        return array();
    }

    static public function globalCount()
    {
        $sql = SQL::getInstance()->getConnection();
        $query = "SELECT COUNT(IdDate) FROM Date ";
        $c = $sql->query($query);
        $result = $c->fetch(PDO::FETCH_OBJ);
        if (!empty($result))
        {
            return $result->count;
        }
        return 0;
    }

}

?>